/***********************************************************************
/
/  CREATES STAR PARTICLES FROM EXISTING PARTICLES
/
/  written by: John Wise
/  date:       March, 2009
/  modified1:
/
/  NOTES:  negative types mark particles that have just been before 
/          and not been converted into a star particle.
/
************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "ErrorExceptions.h"
#include "macros_and_parameters.h"
#include "typedefs.h"
#include "global_data.h"
#include "Fluxes.h"
#include "GridList.h"
#include "ExternalBoundary.h"
#include "Grid.h"

void InsertStarAfter(Star * &Node, Star * &NewNode);

int grid::FindNewStarParticles(int level)
{

  if (MyProcessorNumber != ProcessorNumber)
    return SUCCESS;

  if (NumberOfParticles == 0)
    return SUCCESS;

  int i;
  Star *NewStar, *cstar;
  bool exists;

  for (i = 0; i < NumberOfParticles; i++)
    if (ParticleType[i] == -PARTICLE_TYPE_SINGLE_STAR ||
	ParticleType[i] == -PARTICLE_TYPE_BLACK_HOLE ||
	ParticleType[i] == -PARTICLE_TYPE_CLUSTER ||
	ParticleType[i] == -PARTICLE_TYPE_COLOR_STAR) {

      // Check if it already exists (wasn't activated on the last
      // timestep, usually because of insufficient mass)
      exists = false;
      for (cstar = Stars; cstar; cstar = cstar->NextStar)
	if (cstar->Identifier == ParticleNumber[i]) {
	  exists = true;
	  break;
	}

      if (!exists) {
	NewStar = new Star(this, i, level);
	InsertStarAfter(Stars, NewStar);
	NumberOfStars++;
      }

    }

  return SUCCESS;

}
